var express = require('express')
var router = express.Router()

// 导入db ,操作数据库
const { getAll, getById, exec } = require('../db/index')

/**
 * 获取所有用户
 * GET /blog
 */
router.get('/', async (req, res) => {
  // console.log(req);
  console.log(req.query)
  // 解析请求参数
  // 解构query对象,提取page,size
  // 如果没有值,就给默认值1,2,
  // 如果keyword没有值  默认为''
  const { page = 1, size = 2, keyword = '.' } = req.query

  // 获取总数
  let sql = `select count(*) as total  from gg_user`
  const obj = await getById(sql) // { total: 10 }
  console.log(obj)

  // 根据分页公式, 计算offset偏移量
  let offset = (page - 1) * size
  // 操作数据库
  sql = `select * from gg_user order by user_id desc  limit ${offset}, ${size}`
  const data = await getAll(sql)

  /* 根据关键词查询用户 */
  let sql2 = `SELECT * FROM gg_user WHERE user_name REGEXP '${keyword}' OR user_type REGEXP '${keyword}' order by user_id desc`
  const data2 = await getAll(sql2)

  res.send({
    code: 0,
    message: '获取用户成功',
    result: {
      total: obj.total,
      data: data,
      keywordData: data2,
    },
  })
})

/**
 * 新增
 */

router.post('/', async (req, res) => {
  const { user_name, user_password, user_type } = req.body
  // const { page = 1, size = 2 } = req.query;
  let sql = `insert into gg_user (user_name,user_password,user_type)  values  ('${user_name}', '${user_password}','${user_type}')`
  const data = await exec(sql)
  // console.log(data)
  res.send({
    code: 0,
    message: '新增用户成功',
    result: {
      user_id: data.insertId,
      user_name,
      user_password,
      user_type,
    },
  })
})

// 根据id获取
router.get('/:id', async (req, res) => {
  const { id } = req.params
  let sql = `select * from gg_user where user_id = ${id}`
  const data = await getById(sql)
  res.send({
    code: 0,
    message: '获取用户成功',
    result: {
      data,
    },
  })
})

// 根据用户查密码
router.post('/username', async (req, res) => {
  const { user_name } = req.body
  let sql = `select user_id,user_name,user_password from gg_user where user_name = '${user_name}'`
  const data = await getById(sql)
  if (data) {
    res.send({
      code: 0,
      message: '获取用户成功',
      result: data,
    })
  } else {
    res.send({
      code: 1001,
      message: '没有该用户',
    })
  }
})

// 根据id修改账户密码请求
router.put('/:id', async (req, res) => {
  const { id } = req.params
  const { user_name, user_password } = req.body
  let sql = `update gg_user set user_name='${user_name}',
                                 user_password='${user_password}'
                                  
              where user_id=${id} `
  const data = await exec(sql)
  res.send({
    code: 0,
    message: '修改用户成功',
    result: {
      user_id: id,
      user_name,
      user_password,
    },
  })
})

// 删除
router.delete('/:id', async (req, res) => {
  const { id } = req.params
  let sql = `delete from gg_user where user_id=${id}`
  await exec(sql)
  res.send({
    code: 0,
    message: '删除成功',
    result: '',
  })
})

/* 根据旧的用户名修改新用户名和新密码 */
router.put('/', async (req, res) => {
  console.log(req.body)
  const { oldusername, newusername, newpwd } = req.body
  let sql = `update gg_user set user_name='${newusername}',user_password='${newpwd}' where user_name='${oldusername}'`
  const data = await exec(sql)
  res.send({
    code: 0,
    message: '修改用户成功',
    result: {
      data,
      username: newusername,
      userpassword: newpwd,
    },
  })
})

/* 根据用户名删除用户 */
router.delete('/', async (req, res) => {
  console.log(req.body)
  const { username } = req.body
  let sql = `delete from gg_user where user_name='${username}'`
  await exec(sql)
  res.send({
    code: 0,
    message: '删除成功',
    result: '',
  })
})
module.exports = router
